<!doctype html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="/A0723/include :: header('注册')"/>
</head>
<body>
<header class="mui-bar mui-bar-nav">
    <a href="toLogin" class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
    <h1 class="mui-title">注册</h1>
</header>
<div class="mui-content">
    <div class="forget_from">
        <div class="mui-input-row">
            <label>手机号：</label>
            <input type="text" id="phone" placeholder="请输入手机号">
        </div>
        <div class="mui-input-row">
            <label>验证码：</label>
            <input type="text" id="noteCode" placeholder="请输入短信验证码">
            <button type="button" class="mui-btn mui-btn-blue yzm" onclick="djs()">获取验证码</button>
        </div>
        <div class="mui-input-row">
            <label>密码：</label>
            <input type="password" id="password" placeholder="请输入新密码">
        </div>
        <div class="mui-input-row">
            <label>确认密码：</label>
            <input type="password" id="checkPassword" placeholder="请再次输入密码">
        </div>
        <div class="mui-input-row">
            <label>推荐码：</label>
            <input type="text" id="referralCode" placeholder="请输入推荐码">

        </div>
    </div>
    <!--  -->
    <button type="button" onclick="register()" class="mui-btn mui-btn-blue mui-btn-block mui_btn">确定</button>
    <!--  -->
    <div class="has_user">
        已有账号，<a href="toLogin">去登录</a>
    </div>
</div>
<th:block th:include="/A0723/include :: footer"/>
<th:block th:include="/A0723/include :: common"/>
<script type="text/javascript">
    $(function () {
        let s = GetQueryString('referralCode');
        $("#referralCode").val(s)
    })

    /**
     * 获取url参数值
     * @return {string}
     */
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
        var context = "";
        if (r != null)
            context = r[2];
        reg = null;
        r = null;
        return context == null || context === "" || context === "undefined" ? "" : context;
    }

    let data = {}

    //验证
    function validateRegister() {
        let phone = $("#phone").val();
        if (phone == null || phone === '') {
            mui.toast('请输入手机号', {duration: 'long', type: 'div'})
            return
        }

        let password = $("#password").val();
        if (password == null || password === '') {
            mui.toast('请输入密码', {duration: 'long', type: 'div'})
            return
        }
        let checkPassword = $("#checkPassword").val();
        if (checkPassword == null || checkPassword === '') {
            mui.toast('请输入确认密码', {duration: 'long', type: 'div'})
            return
        }
        if (password !== checkPassword) {
            mui.toast('确认密码与密码不一致', {duration: 'long', type: 'div'})
            return
        }

        let referralCode = $("#referralCode").val();
        if (referralCode == null || referralCode === '') {
            mui.toast('请输入推荐码', {duration: 'long', type: 'div'})
            return
        }

        let noteCode = $("#noteCode").val();
        if (noteCode == null || noteCode === '') {
            mui.toast('请输入短信验证码', {duration: 'long', type: 'div'})
            return
        }
        data.phone = phone;
        data.referralCode = referralCode;
        data.password = password;
        data.checkPassword = checkPassword;
        data.noteCode = noteCode;
        return true;

    }

    function register() {
        if (validateRegister()) {
            mui.ajax(ctx + 'front/release/register', {
                data: data,
                dataType: 'json',//服务器返回json格式数据
                type: 'post',//HTTP请求类型
                timeout: 10000,//超时时间设置为10秒；
                headers: {'Content-Type': 'application/json'},
                success: function (res) {
                    //服务器返回响应，根据响应结果，分析是否登录成功；  ctx + 'front/release/toLogin';
                    mui.toast(res.msg, {duration: 'long', type: 'div'})
                    if (res.code === 0) {
                        setInterval(function () {
                            window.location.href=document.referrer;
                        }, 2000)
                    }
                },
                error: function (xhr, type, errorThrown) {
                    //异常处理；
                    setInterval(function () {
                        closeMask()
                    }, 2000)
                    console.log(type);
                }
            });
        }
    }

    //发送验证码 倒计时
    function djs() {
        $('.yzm').attr("disabled", true); //禁用发送验证码按钮
        var start = 180;
        var time = document.querySelector('.yzm');
        //let phone = $("#phone").val();
        //sendMsg(phone);
        var interval = setInterval(function () {
            start--;
            if (start < 0) {
                start = 0;
                $('.yzm').attr("disabled", false); //恢复发送验证码按钮
                $(".tis_djs").hide(); //计数为零提示语隐藏
                clearInterval(interval)  //清除定时器
            }
            time.innerHTML = '重新发送' + start + 's';
        }, 1000)
    }
</script>
</body>

</html>
